//
//  BMIController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/9.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "BMIController.h"
#import "Config.h"

@interface BMIController ()

@property (weak, nonatomic) IBOutlet UILabel *resultText;
@property (weak, nonatomic) IBOutlet UITextField *heightField;
@property (weak, nonatomic) IBOutlet UITextField *weightField;
@property (weak, nonatomic) IBOutlet UIButton *resultButton;

@end

@implementation BMIController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"BMI自测";
    [self setViewAttrWithView:self.heightField];
    [self setViewAttrWithView:self.weightField];
    self.resultButton.layer.cornerRadius = 5.0f;
    self.resultButton.layer.masksToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setViewAttrWithView:(UIView *)view {
    view.layer.cornerRadius = 5.0f;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = 0.5f;
    view.layer.borderColor = XHConfigColor.CGColor;
}

#pragma mark - 查看结果
- (IBAction)seeResult:(UIButton *)sender {
    if (self.heightField.text.length < 1)
        [self setAlertViewWithMessage:@"请输入您的身高(厘米)!"];
    else if (self.weightField.text.length < 1)
        [self setAlertViewWithMessage:@"请输入您的体重(公斤)!"];
    else
        [self calculateResult];
}

- (void)calculateResult  {
    [self.view endEditing:NO];
    CGFloat height = [self.heightField.text floatValue];
    CGFloat weight = [self.weightField.text floatValue];
    CGFloat result = weight / (height / 100 * height / 100);
    
    for (UIView *view in self.view.subviews)
        if (view != self.resultText)
            view.hidden = YES;
    
    if (result >= 25 && result <= 28)
        self.resultText.text = [NSString stringWithFormat:@"您的BMI值是：%.f，您的体重状况为：轻度肥胖，请适当加强体育锻炼，调整食物摄入和饮食习惯。", result];
    else if (result < 20)
        self.resultText.text = [NSString stringWithFormat:@"您的BMI值是：%.f，您的体重状况为：体重过轻，请适当提高营养摄入，加强体育锻炼。", result];
    else if (result > 28)
        self.resultText.text = [NSString stringWithFormat:@"您的BMI值是：%.f，您的体重状况为：体重超重，请加强体育锻炼，调整食物摄入和饮食习惯，必要时请及时就医，检查是否有心血管疾病，高血压，高胆固醇等疾病。", result];
    else if (result < 25 && result >= 20)
        self.resultText.text=[NSString stringWithFormat:@"您的BMI值是：%.f，您的体重状况为：体重正常，患有心血管疾病，高血压，高血脂等疾病风险极低，请继续保持。", result];
}

#pragma mark - AlertView
- (void)setAlertViewWithMessage:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:NO];
}

@end
